<?php
namespace Mall\Controller;

use Common\Controller\ApiController;
use Common\Model\ModelException;
use Util\ModelUtils;

class CartController extends ApiController
{
    public function index()
    {
        $cart = D('Cart')->getByUserId($this->user['id']);
        $items = $cart['items'];
        $goodsIds = array_keys($items);
        if ($goodsIds) {
            $goodss = D('Goods')->where(['goods_id' => ['in', $goodsIds]])->select();

            $shopIds = array_column($goodss, 'shop_id');
            $shops = D('Shop')->where(['shop_id' => ['in', $shopIds]])->select();
            $cartList = [];
            ModelUtils::belongsTo($goodss, ['foreignKey' => 'cate_id', 'relation' => 'GoodsCate', 'refKey' => 'cate_id', 'name' => 'cate']);
            $goodss = transform('Goods', 'cart', $goodss);
            $shops = transform('Shop', 'listview', $shops);
            $goodss = array_column($goodss, null, 'goods_id');
            $cartGoodss = [];
            foreach ($items as $goodsId => $itemInfo) {
                foreach ($itemInfo as $skuname => $value) {
                    $cartGoodss[] = array_merge($goodss[$goodsId], $value);
                }
            }
            

            $goodsGroup = array_group($cartGoodss, 'shop_id');
            foreach ($shops as $shop) {
                $cartList[] = [
                    'shop' => $shop,
                    'goodslist' => $goodsGroup[$shop['shop_id']],
                ];
            }
        }

        $this->response($cartList ?: []);

    }

    public function pick()
    {
        $input = $this->request();
        $user = $this->user;
        $checkUser = D('Users')->find($user['id']);
       
        // if($checkUser['role']==3 || $checkUser['role']==2){
        //     $companyCert = D('CompanyCert')->where(['user_id'=>$user['id']])->find();
        //     if($companyCert['status']!=1){
        //         $this->errorResponse(90122);
        //     }
        // }
        $goodsId = $input['goods_id'];
        $quantity = filter($input['quantity'], 1);
        $act = filter($input['act'], 'increment');
        $this->checkNull($input, ['goods_id', 'sku']);
        $sku = $input['sku'];

        $userId = $this->user['id'];
        $goodsList = [];
        $goodsList[] = [
            'goods_id' => $goodsId,
            'quantity' => $quantity,
            'sku' => $sku
        ];
        if ($act == 'reduce') {
            D('Cart')->putOut($userId, $goodsList);
        } else {
            D('Cart')->putIn($userId, $goodsList);
        }
        $cart = D('Cart')->getByUserId($userId);
        $this->response($cart);
    }

    public function remove()
    {
        $input = $this->request();
        $goodsIds = $input['goods_ids'] === '' ? [] : explode(',', $input['goods_ids']);
        $skus = $input['skus'] === '' ? [] : explode(',', $input['skus']);
        if ($goodsIds) {
            $goodsList = [];
            foreach ($goodsIds as $index => $goodsId) {
                $goodsList[] = ['goods_id' => $goodsId, 'quantity' => 999, 'sku' => $skus[$index]];
            }
            D('Cart')->putOut($this->user['id'], $goodsList);
        }
        $this->modifySuccessResponse();
    }


}
